<?php
	class BaseconfigControl extends CommonControl{
		function configset(){

			$tplDirs = dir::tree("./template");
			// p($tplDirs);die;
			$tpls = array();
			$id=0;
			foreach ($tplDirs as $tpl) {
				$xml = $tpl['path'].'/read.xml';
				$tpls[$id]=array();

				if (is_file($xml)) {
	                //获得模板风格的URL地址，用于显示模板图片
	                $tplUrl = __ROOT__.'/template/'.basename($tpl['path']);
	                //获得模板风格的XML定义
	                $tpls[$id] = xml::toArray(file_get_contents($xml));
	                //模板截图
	                $tpls[$id]['short']=$tplUrl .'/'.basename($tpls[$id]['short']);
	                //模板风格名称
	                $tpls[$id]['style']=basename($tpl['path']);
            	}
            	$id++;
			}

			$this->assign("defaultjpg",__ROOT__.'/static/default.png');
			$this->assign('tpl_styles',$tpls);
        	//分配置所有模板风格数据
        	$this->assign('tpl_styles',$tpls);
        	$db = M("config");
        	$tpl_config = $db->where("name='style'")->find();
        	$this->assign("tpl_config", $tpl_config);

        	$sql1="select value from ".C('DB_PREFIX')."config where cid=5";
       		$kg=M()->query($sql1);
       		$this->assign('kg',$kg);
			$sql="select name,title,value,message from ".C('DB_PREFIX')."config where cid<4";
			$result=M()->query($sql);
			$this->assign('result',$result);
			$this->display();
			
		}

		function configedit(){
			$data=$_POST;
			$db=M('config');
			foreach ($data as $k=>$v) {
				$sql="update ".C('DB_PREFIX')."config set value='$v' where name='$k'";
				$db->exe($sql);
			}
			$allConfig=$db->field('name,value')->all();
			// p($allConfig);die;
			$dataConfigfile = array();
			foreach ($allConfig as $conf) {
				$dataConfigfile[$conf['name']]=$conf['value'];
			}
			$str = "<?php if(!defined('PATH_HD'))exit;\nreturn ".var_export($dataConfigfile,true).";\n?>";
	         if(@file_put_contents("./config/baseconfig.php", $str)){
	            $this->success("修改配置项成功 ");
	         }else{
	             $this->error("配置文件不能写，请修改文件权限");
	         }
		}
		//编辑时显示验证码
		function codeshow(){
			$config=include PATH_ROOT.'/config/baseconfig.php';
			$this->assign('config', $config);
			$this->display();
		}
		//修改验证码
		function codeedit(){
			$file=PATH_ROOT.'/config/baseconfig.php';
			$config = include $file;
			$data=array_merge($config,$_POST);
			// p($data);die;
			$str = "<?php\r\nreturn ";
			$str .= var_export($data, true);
			$str .= ";\r\n?>";
			if (@file_put_contents($file, $str)) {
				$this->success("修改成功");
			}
		}

		function watershow(){
			$config=include PATH_ROOT.'/config/baseconfig.php';
			$this->assign('config', $config);
			$this->display();
		}

		function wateredit(){
			$file=PATH_ROOT.'/config/baseconfig.php';
			$config = include $file;
			$data=array_merge($config,$_POST);
			$str = "<?php\r\nreturn ";
			$str .= var_export($data, true);
			$str .= ";\r\n?>";
			if (file_put_contents($file, $str)) {

				$this->success("修改成功");
			}
		}
	}
?>